import {ArrMethods} from "./arr"
export function observer(data){
  if(typeof data !== 'object' || data==null){
    return
  }
  return new Observer(data)
}
class Observer{
  constructor(data){
    Object.defineProperty(data,"__ob__",{
      enumerable:false,
      value:this
    })
    if(Array.isArray(data)){
      data.__proto__ = ArrMethods
      this.observerArray(data)
    }else{
      this.walk(data)

    }
  }
  observerArray(data){
    data.forEach(item => {
      observer(item)
    })
  }
  walk(data){
    let keys = Object.keys(data)
    for(let i=0;i<keys.length;i++){
      let key = keys[i]
      let value = data[key]
      defineReactive(data,key,value)
    }
  }
}
function defineReactive(target,key,value){
  console.log(target,key,value)
  observer(value)
  Object.defineProperty(target,key,{
    get(){
      console.log(`获取${key}的值为${value}`)
      return value
    },
    set(newValue){
      if(value === newValue) return 
      value = newValue
      observer(newValue)
    }
  })
}